<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Ticketschecked extends Controller
{

    /**
     * PDA授权
     * @author zhixuedong
     * @date 2021/2/27/027
     */
    public function authorization(){
        try {
            $config = Db::name("tickets_config")->field("appKey,appSecret,headip,headipbak,headipport,auto,time")->find();
            if(empty($config)){
                throw new Exception("配置不存在");
            }
            $black['code'] = 0;
            $black['data'] = $config;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    public function quit()
    {
        try {
            Session::delete("showuser");
            $black['code'] = 0;
            $black['msg'] = "退出成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 登录
     * @return false|string
     * @author zhixuedong
     * @date 2021/2/25/025
     */
    public function login()
    {
        try {
            $rule = [
                ["account", "require", "账号不能为空"],
                ["password", "require", "密码不能为空"],
                ["appid", "require", "Appid不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $appid = $data['appid'];
            $config = Db::name("config")->where(['xcx_appid' => $appid])->find();

            $accountData['uniacid'] = $config['id'];
            $accountData['account'] = $data['account'];
            $accountData['passwd'] = md5($data['password']);
            $accountData['status'] = 1;
            $account = Db::name("account")->where($accountData)->field("id,account")->find();
            if (empty($account)) {
                throw new Exception("账号不存在或密码错误");
            } else {
                Session::set("showuser", $account['id']);
            }
            $black['user'] = $account;
            $black['code'] = 0;
            $black['msg'] = "登录成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 刷身份证
     * @author zhixuedong
     * @date 2021/2/25/025
     */
    public function idcard()
    {
        try {
//            $showuser = Session::get("showuser");
//            if (empty($showuser)) {
//                throw new Exception("请先登录");
//            }

            $rule = [
                ["idcard", "require", "身份证不能为空"],
                ["deviceSerial", "require", "设备号不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //查询最新一次卡片
            $detail = Db::name("tickets_list")->where(['IDcard' => $data['idcard'], 'type' => ['gt', 1]])->order("id desc")->find();
//            if(empty($detail)){
//                $detail = Db::name("tickets_list")->where(['IDcard'=>$data['idcard']])->order("id desc")->find();
//            }

            if ($detail['degree'] > 0) {
                $nowtime = time();
                $degree = $detail['degree'] - 1;
                $data['check_time'] = $nowtime;
                $data['degree'] = $degree;
                Db::name("tickets_list")->where(['IDcard' => $data['idcard']])->update($data);
                if ($degree == 0) {
                    $commondevice = new Test();
                    $commondevice->cartDel($detail['signcode']);
                }


                $log['date_time'] = date("Y-m-d H:i:s");
                $log['employee_no'] = $data['signcode'];
                $log['card_no'] = $detail['idcard'];
                $log['device_serial'] = $data['deviceSerial'];
                $log['device_name'] = $data['deviceSerial'];
                $log['type'] = "app检票:刷身份证";
                $log['description'] = "app检票:刷身份证";
                $log['code'] = "200";
                Db::name("tickets_device_log")->insert($log);

//              if($nowtime>strtotime('+1minute',$detail['check_time'])){
//
//              }

            } else {
                throw new Exception("无效卡");
            }

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 刷二维码
     * @author zhixuedong
     * @date 2021/2/25/025
     */
    public function qrcode()
    {
        try {
//            $showuser = Session::get("showuser");
//            if(empty($showuser)){
//                throw new Exception("请先登录");
//            }
            $rule = [
                ["signcode", "require", "二维码不能为空"],
                ["deviceSerial", "require", "设备号不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            //查询最新一次卡片
            $detail = Db::name("tickets_list")->where(['signcode' => $data['signcode']])->order("id desc")->find();
            $nowtime = time();
            if ($detail['degree'] > 0) {
                $degree = $detail['degree'] - 1;
                $data['check_time'] = $nowtime;
                $data['degree'] = $degree;
                Db::name("tickets_list")->where(['signcode' => $data['signcode']])->update($data);
                if ($degree <= 0) {
                    $commondevice = new Test();
                    $commondevice->cartDel($detail['signcode']);
                }

                $log['date_time'] = date("Y-m-d H:i:s");
                $log['employee_no'] = $data['signcode'];
                $log['card_no'] = $detail['signcode'];
                $log['device_serial'] = $data['deviceSerial'];
                $log['device_name'] = $data['deviceSerial'];
                $log['type'] = "app检票:刷二维码";
                $log['description'] = "app检票:刷二维码";
                $log['code'] = "200";
                Db::name("tickets_device_log")->insert($log);
//                if($nowtime>strtotime('+1minute',$detail['check_time'])){}
            } else {
                throw new Exception("无效卡");
//                if($nowtime>strtotime('+1minute',$detail['check_time'])){}
            }
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    public function loglist()
    {
        try {
//            $showuser = Session::get("showuser");
//            if(empty($showuser)){
//                throw new Exception("请先登录");
//            }
            $rule = [
                ["deviceSerial", "require", "设备号不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $list = Db::name("tickets_device_log")->where(['device_serial'=>$data['deviceSerial']])->limit(100)->field("id,card_no,date_time")->select();
            foreach ($list as $k=>$v){
                $str = $v['card_no'];
                $len = strlen($str);
                $list[$k]['card_no'] = str_repeat('*', $len - 4).substr($str, -4);
            }

            $black['code'] = 0;
            $black['list'] = $list;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}